import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/controllers/user_controller.dart';
import 'package:flutter_demo/pages/download_list_page.dart';
import 'package:flutter_demo/pages/fav_list_page.dart';
import 'package:flutter_demo/pages/login_page.dart';
import 'package:get/get.dart';

import '../utils/const.dart';

class UserCenterPage extends GetView<UserController> {
  const UserCenterPage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return ListView(
      physics: const BouncingScrollPhysics(),
      children: _buildContent(),
    );
  }

  _handleToSetting() {}

  _handleToLogin() {
    Get.to(() => LoginPage());
  }

  Widget _buildHeader() {
    return Container(
      color: Colors.blue,
      height: 200,
      child: Center(
        heightFactor: 200,
        child: Obx(
          () => Column(
            children: [
              ClipOval(
                child: CachedNetworkImage(
                  imageUrl: controller.isLogin()
                      ? "${controller.getUser().avatar}"
                      : "https://fourpetal.com/wp-content/themes/ripro/assets/images/avatar/1.png",
                  httpHeaders: Const.imgHeaders,
                  fit: BoxFit.cover,
                  width: 64,
                  height: 64,
                ),
              ),
              GestureDetector(
                onTap: _handleToLogin,
                child: Text(
                  controller.isLogin() ? "${controller.getUser().nickname}" : "去登陆",
                  style: TextStyle(color: Get.theme.secondaryHeaderColor, fontSize: 20),
                ).marginOnly(top: 20),
              ),
            ],
          ),
        ),
      ),
    );
  }

  List<Widget> _buildContent() {
    return [
      _buildHeader(),
      Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          InkWell(
            onTap: () {
              Get.to(() => const DownloadListPage());
            },
            child: Column(
              children: [
                const Icon(Icons.download).paddingOnly(bottom: 5),
                const Text("我的下载"),
              ],
            ).paddingAll(13),
          ),
          Container(width: 0.5, height: 30, color: Get.theme.dividerColor),
          InkWell(
            onTap: () {
              Get.to(() => const FavListPage());
            },
            child: Column(
              children: [
                const Icon(Icons.favorite).paddingOnly(bottom: 5),
                const Text("我的收藏"),
              ],
            ).paddingAll(13),
          ),
        ],
      ),
      const Divider(height: 0.5),
      ListTile(
        title: const Text("设置"),
        leading: const Icon(Icons.settings),
        trailing: const Icon(Icons.arrow_forward_ios, size: 18),
        onTap: _handleToSetting,
      ),
      const Divider(height: 0.5),
      ListTile(
        title: const Text("关于"),
        leading: const Icon(Icons.info),
        trailing: const Icon(Icons.arrow_forward_ios, size: 18),
        onTap: _handleToSetting,
      ),
      const Divider(height: 0.5),
    ];
  }
}
